#include <linux/errno.h>
#include <linux/fs2.h>

int vfs_readdir(struct file *file, filldir_t filler, void *buf)
{
	struct inode *inode = file->f_path.dentry->d_inode;
	int res = -ENOTDIR;

	printf("this is %s(): %d\r\n", __func__, __LINE__);


	if (!file->f_op || !file->f_op->readdir)
		goto out;

	printf("this is %s(): %d\r\n", __func__, __LINE__);
	// res = security_file_permission(file, MAY_READ);
	// if (res)
	// 	goto out;

	// res = mutex_lock_killable(&inode->i_mutex);
	// if (res)
	// 	goto out;

	// res = -ENOENT;
	// if (!IS_DEADDIR(inode)) {
	printf("this is %s(): %d\r\n", __func__, __LINE__);
		res = file->f_op->readdir(file, buf, filler);
	printf("this is %s(): %d\r\n", __func__, __LINE__);
		// file_accessed(file);
	// }
	// mutex_unlock(&inode->i_mutex);
out:
	printf("this is %s(): %d\r\n", __func__, __LINE__);
	return res;
}

unsigned int sys_readdir(int fd, struct dirent *dirent)
{
	// romfs_readdir(NULL, NULL, NULL);
	int i;
	struct file *filp;
	struct dirents dirents = {.num = 0, .dirent = dirent};

	printf("this is %s(): %d >>> fd = %d\r\n", __func__, __LINE__, fd);

	// filp = do_filp_open(0, "/bin", 0 , 0, 0);

	filp = fget_light(fd, NULL);

    vfs_readdir(filp, NULL, &dirents);

	for (i = 0; i < dirents.num; i++) {
		// printf("this is %s(): %d >>> dirents.dirent[%d].d_name = %s\r\n", __func__, __LINE__, 
		// 			i, dirents.dirent[i].d_name);
		printf("this is %s(): %d >>> dirent[%d].d_name = %s\r\n", __func__, __LINE__, 
					i, dirent[i].d_name);
	}

    return dirents.num;
}